package com.whfc.fuum.service.impl;


import com.whfc.fuum.dao.SysUserMapper;
import com.whfc.fuum.dao.SysUserRuleFastMapper;
import com.whfc.fuum.dto.SysRuleDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.SysUserRuleFast;
import com.whfc.fuum.param.SysRuleFastAdd;
import com.whfc.fuum.service.SysUserRuleFastService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*
*/
@DubboService(interfaceClass = SysUserRuleFastService.class, version = "1.0.0")
public class SysUserRuleFastServiceImpl implements SysUserRuleFastService {

    @Autowired
    private SysUserRuleFastMapper sysUserRuleFastMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysRuleDTO> findByUserIdAndDeptId(Integer userId, Integer deptId) {
        List<SysRuleDTO> byUserIdAndDeptId = sysUserRuleFastMapper.findByUserIdAndDeptId(userId, deptId);
        for (SysRuleDTO sysRuleDTO : byUserIdAndDeptId) {
            if (sysRuleDTO.getType() == 1) {
                List<SysRuleDTO> childList = sysUserRuleFastMapper.findByParentIdAndType(sysRuleDTO.getRuleId(), 2);
                sysRuleDTO.setChildList(childList);
            }
        }
        return byUserIdAndDeptId;
    }

    @Override
    public List<SysRuleDTO> findAllByUserIdAndDeptId(Integer userId,Integer deptId) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
        if (sysUser.getType()==1) {
            userId = null;
        }
        return sysUserRuleFastMapper.findAllByUserIdAndDeptId(userId,deptId);
    }

    @Override
    public void addUserRuleFast(SysRuleFastAdd sysRuleFastAdd) {
        // 删除原有
        sysUserRuleFastMapper.deleteByUserIdAndDeptId(sysRuleFastAdd.getUserId(),sysRuleFastAdd.getDeptId());
        List<Integer> ruleIds = sysRuleFastAdd.getRuleIds();
        SysUserRuleFast sysUserRuleFast = new SysUserRuleFast();
        for (Integer ruleId : ruleIds) {
            sysUserRuleFast = new SysUserRuleFast();
            sysUserRuleFast.setUserId(sysRuleFastAdd.getUserId());
            sysUserRuleFast.setDeptId(sysRuleFastAdd.getDeptId());
            sysUserRuleFast.setRuleId(ruleId);
            sysUserRuleFast.setCreateTime(new Date());
            sysUserRuleFastMapper.insertSelective(sysUserRuleFast);
        }
    }
}
